<!DOCTYPE html>
<html>
<head>
    <th:block th:include="include :: header('连接记录')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">连接名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="connName"  autocomplete="off" lay-affix="clear" placeholder="连接名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">数据库</label>
                <div class="layui-input-inline">
                    <select name="dbType"  lay-filter="selected" lay-search>
                        <option value=""></option>
                        <option value="MySQL">MySQL</option>
                        <option value="Oracle">Oracle</option>
                        <option value="SQLServer">SQLServer</option>
                        <option value="PostgreSQL">PostgreSQL</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-inline">
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="source-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="source-table" lay-filter="source-table"></table>
    </div>
</div>

<script type="text/html" id="source-toolbar">
    <button class="pear-btn pear-btn-md pear-btn-primary" lay-event="add-source">
        <i class="layui-icon pear-icon pear-icon-add"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="source-bar">
    <a class="pear-btn pear-btn-primary pear-btn-xs" lay-event="conn">测试</a>
    <a class="pear-btn pear-btn-warming pear-btn-xs" lay-event="edit">编辑</a>
    <a class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除</a>
</script>

<script type="text/html" id="source-status">
    <input type="checkbox" name="enable" value="{{d.userId}}" lay-skin="switch" lay-text="解锁|锁定"
           lay-filter="user-status" {{ d.status== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="source-enable">
    <input type="checkbox" name="enable" value="{{d.userId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="user-enable" {{ d.enable== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="source-sex">
    {{#if (d.sex == 1) { }}
    <span>男</span>
    {{# }else if(d.sex == 2){ }}
    <span>女</span>
    {{# } }}
</script>

<script type="text/html" id="source-login">
    {{#if (d.login == 0) { }}
    <span>在线</span>
    {{# }else if(d.sex == 1){ }}
    <span>离线</span>
    {{# } }}
</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd hh:MM:ss')}}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common','http'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let http = layui.http;

        let MODULE_PATH = "/datasource/";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '连接名称',
                    field: 'connName',
                    align: 'center',
                    width: 120
                },
                {
                    title: 'data类型',
                    field: 'dbType',
                    align: 'center'
                },
                {
                    title: '连接URL',
                    field: 'connUrl',
                    align: 'center'
                },
                {
                    title: '用户名称',
                    field: 'userName',
                    align: 'center'
                },
                {
                    title: '用户密码',
                    field: 'userPwd',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#source-bar',
                    align: 'center',
                    width: 180
                }
            ]
        ]
        const tableId = "source-table"
        table.render({
            elem: '#source-table',
            url: MODULE_PATH + "page",
            page: true,
            cols: cols,
            method:'post',
            skin: 'line',
            toolbar: '#source-toolbar',
            text: {none: '暂无信息'},
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(source-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
            else if (obj.event === 'conn'){
                window.testConn(obj)
            }
        });

        table.on('toolbar(source-table)', function (obj) {
            if(obj.event === 'add-source'){
                window.import()
            }
            else if (obj.event === 'refresh') {
                window.refresh();
            }
            else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(source-query)', function (data) {
            table.reload('source-table', {
                where: data.field,
            })
            return false;
        });


        form.on('switch(source-status)', function (obj) {
            let status = this.checked ? 1 : 0
            let param = {userId:this.value,status:status}
            updateUser(param)
        });

        form.on('switch(source-enable)', function () {
            let enable = this.checked ? 1 :0
            let param = {userId:this.value,enable:enable}
            updateUser(param)
        });

        window.import = function (){
            const size = common.getWindowSize();
            layer.open({
                type: 2,
                title: '新增数据源',
                shade: 0.1,
                area: [size.width, size.height],
                content: MODULE_PATH + 'source-add.html'
            })
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改数据源',
                shade: 0.1,
                area: ['500px', '90%'],
                content: MODULE_PATH +'source-edit?id='+obj.data["id"]
            });
        }

        window.testConn = function (obj){
            const loadIndex = layer.load(0);
            http.get(MODULE_PATH + "test/"+obj.data["id"],{}).then(res=>{
                if(res.success){
                    layer.close(loadIndex)
                    layer.msg(res.message);
                }
            }).catch(e=>{
                layer.msg(e.message);
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该信息', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let param = [obj.data['id']]
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:param,
                    method:"delete"
                }).then(res=>{
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }

        window.batchRemove = function (obj) {
            let checkIds = table.checkStatus(obj.config.id).data;
            if (checkIds.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = [];
            for(let i = 0;i<checkIds.length;i++){
                ids.push(checkIds[i].id)
            }
            layer.confirm('确定要删除这些数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                http.request({
                    url: MODULE_PATH + "delete",
                    contentType: http.contentType.json,
                    data:ids,
                    method:"delete"
                }).then(res=>{
                    layer.close(index);
                    layer.msg(res.message, {icon: 1,time: 1000}, function () {
                        table.reload(tableId);
                    });
                })
            });
        }

        window.updateUser = function (obj){
            let loading = layer.load();
            // 修改状态
            $.post("/system/user/updateUserState",obj,function (res){
                layer.close(loading);
                if(res.code === 200){
                    layer.msg("用户状态更新成功");
                }
                else{
                    layer.msg(res.message, {
                        icon: 2,
                        time: 3000
                    });
                }
            })
        }

        window.refresh = function (param) {
            table.reload(tableId);
        }
    })
</script>
</body>
</html>
